﻿
(function (window, undefined) {

    var $ = window.$, app = {};

    app.settings = {
        serivceUrl: "",
        appid: "appidTest",
        currentUser: "F459DEF9-6D6B-E611-9F24-4CCC6A479139"//结合appkey加密过的
    };

    app.masterData = {};
    app.code2Text = function (category, code) {
        /// <summary>代码项转换为文字项</summary>
        /// <param name="category">分类</param>
        /// <param name="code">代码</param>
        /// <returns type="string">文字项</returns>
        if (app.masterData[category] == null) {
            return "";
        }

        var outPut = JSLINQ(app.masterData[category])
            .Where(function (item) { return item.code === code; })
            .Select(function (item) { return item.name; });

        if (outPut.items.length === 0) {
            return "";
        }
        return outPut.items[0];
    }

    app.ajax = function (setting) {
        //还有api路径而且没有域名
        if (setting.url.indexOf('api/') > -1 && setting.url.indexOf('api/') < 5) {
            setting.url = window.app.settings.serivceUrl + setting.url;
        }
        if (typeof setting.headers === "undefined") {
            setting.headers = {};
        }
        setting.headers["appid"] = window.app.settings.appid;
        setting.headers["currentUser"] = window.app.settings.currentUser;


        //if (!setting.beforeSend) {
        //    setting.beforeSend = function (xhr) {
        //        //xhr.setRequestHeader("appId", "appTest");
        //    }
        //}
        return jQuery.ajax(setting);
    }
    app.get = function (url, data, callback, type) {
        return app.ajax({
            url: url,
            type: "GET",
            dataType: type,
            data: data,
            success: callback
        });
    };
    app.getJSON = function (url, data, callback) {
        /// <summary>获取json。</summary>
        /// <param name="url">url的完整地址或者是服务的相对路径</param>
        /// <param name="data">数据参数</param>
        /// <param name="callback">回调函数</param>
        /// <returns type="object">jquery的get方法</returns>
        return app.get(url, data, callback, "json");
    }

    app.getMasterData = function () {
        /// <summary>获取主数据</summary>


        window.app.getJSON("/MasterData/GetMasterDataSort", {}, function (data) {

            window.app.getJSON("/MasterData/GetMasterData", {}, function (masterData) {

                for (var index = 0; index < data.length; index++) {
                    var category = data[index].code;
                    var outPut = JSLINQ(masterData)
                        .Where(function (item) { return item.category === category; })
                        .Select(function (item) { return item });
                    app.masterData[category] = outPut.items;
                }

            });
        });
    }

    app.refurbishMasterData = function () {
        /// <summary>刷新主数据</summary>
        window.app.masterData = {};

        window.app.getMasterData();
    }



    if (!window.app) window.app = app;

}(window));


Request =
    {
        QueryString: function (item) {
            var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
            return svalue ? svalue[1] : svalue;
        }
    };
